// pages/classify/classify.js
import { shop, goods, shopping } from "../../utils/http"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shops: [],
    goodss: [],
    shopss: [],
    selected: [],
    options: [],
  },
  // 跳转
  tiaoz(e){
    console.log(e);
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/detail/detail?id=${id}`,
    })
  },
  // 图标
  setbadge(num) {
    wx: wx.setTabBarBadge({
      index: 2,
      text: String(num),
      //  success: (res) => {},
      //  fail: (res) => {},
      //  complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // 加入购物车
  join(e) {
    let id = e.currentTarget.dataset.id
    shopping({
      goodsId: id,
      number: 1,
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res);
      this.setData({
        // number:res.data.number
      })
      this.setbadge(res.data.number)
    })
  },
  // tab栏切换
  tyyy(e) {
    //tab栏切换,获取到id
    let { shopss } = this.data;
    console.log(e)
    let id = e.currentTarget.dataset.id
    console.log(id);
    goods({ categoryId: id }).then((res) => {
      console.log(res);
      if (res.data) {
        shopss = res.data.result
      } else {
        shopss = []
      }
      // console.log(this.data.shop);
      shopss.reverse()
      this.setData({
        shopss
      })
      console.log(shopss);
      console.log(this.data.selected);
    })
    //console.log("index",index)
    this.setData({
      selected: id,
    })

  },
  onLoad: function (options) {
    console.log(options);
    this.data.options = options.id
    let { shops, shopss, selected } = this.data
    shop().then((res) => {
      console.log(res);
      if (res.msg == "success") {
        shops = res.data
        // console.log(this.data.shop);
      }
      this.setData({
        shops
      })
    })
    goods({ categoryId: this.data.options }).then((res) => {
      console.log(res);
      selected = this.data.options
      if (res.code == 0) {
        shopss = res.data.result
      } else if (res.code == 700) {
        shopss = []
      }
      // if(shopss.length>0){
      shopss.reverse()
      // }
      this.setData({
        shopss,
        selected
      })
      console.log(shopss);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})